/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.file.controller.file;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyts.infodb.config.ModuleNameEnum;
import com.hyts.infodb.config.SystemConfiguration;
import com.hyts.infodb.config.SystemConfiguration.MessageConfiguration;
import com.hyts.infodb.file.bean.file.FileBO;
import com.hyts.infodb.file.bean.file.FileDTO;
import com.hyts.infodb.file.bean.file.FileVO;
import com.hyts.infodb.file.service.file.FileService;

/** 
 * @author LiBo/Alex
 * @see com.hyts.infodb.file.service.file.FileService
 * @version V1.0 
 */
@RestController
@RequestMapping("/file")
public class FileController {
    
    @Autowired
    FileService service;
    
    /**
     * <b>查询基本信息</b>
     * @param id
     * @return
     * @exception
     */
    @RequestMapping("/load/{id}")
    public FileDTO loadFileInfo(@PathVariable("id") Long id) {
        FileDTO fileDTO = new FileDTO();
        FileBO bo = new FileBO();
        bo.setId(id);
        FileVO vo = service.loadFile(bo);
        if(vo == null || vo.getContent() == null) {
            return (FileDTO) fileDTO.failure(MessageConfiguration.LOAD,ModuleNameEnum.FILE_MODULE.getModuleName());
        }else {
            return (FileDTO) fileDTO.success(MessageConfiguration.LOAD,ModuleNameEnum.FILE_MODULE.getModuleName(),vo.getContent());
        }
    }
    
    /**
     * <b>保存基本信息</b>
     * @param file
     * @return
     * @exception
     */
    @RequestMapping("/save")
    public FileDTO saveFileInfo(@RequestBody FileBO file) {
        FileDTO fileDTO = new FileDTO();
        boolean result = service.saveFile(file);
        if(result) {
            return (FileDTO) fileDTO.success(MessageConfiguration.SAVE,ModuleNameEnum.FILE_MODULE.getModuleName());
        }else {
            return (FileDTO) fileDTO.failure(MessageConfiguration.SAVE,ModuleNameEnum.FILE_MODULE.getModuleName());
        }
    }
    
    /**
     * <b>删除基本信息</b>
     * @param id
     * @return
     * @exception
     */
    @RequestMapping("/delete/{id}")
    public FileDTO deleteFileInfo(@PathVariable("id") Long id) {
        FileDTO fileDTO = new FileDTO();
        FileBO bo = new FileBO();
        bo.setId(id);
        boolean result = service.deleteFile(bo);
        if(result) {
            return (FileDTO) fileDTO.success(MessageConfiguration.REMOVE,ModuleNameEnum.FILE_MODULE.getModuleName());
        }else {
            return (FileDTO) fileDTO.failure(MessageConfiguration.REMOVE,ModuleNameEnum.FILE_MODULE.getModuleName());
        }
    }
    
    /**  
     * @param file
     * @return
     * @exception
     */ 
    @RequestMapping("/find")
    public FileDTO findFileList(@RequestBody FileBO file) {
        FileVO vo = service.findFileList(file);
        FileDTO fileDTO = new FileDTO();
        if(vo == null) {
            return (FileDTO) fileDTO.failure(MessageConfiguration.LOAD,ModuleNameEnum.FILE_MODULE.getModuleName());
        }else {
            return (FileDTO) fileDTO.success(MessageConfiguration.LOAD,ModuleNameEnum.FILE_MODULE.getModuleName(),vo.getContents());
        }
    }
}
